package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.entity.DecorationConfig;
import com.ala4.oxcafe.repository.mapper.DecorationConfigMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统附件存储实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Repository
public class DecorationConfigRepository extends ServiceImpl<DecorationConfigMapper, DecorationConfig> implements IService<DecorationConfig> {

    public DecorationConfig decorationConfig() {
        return this.getOne(Wrappers.lambdaQuery(), false);
    }

    public DecorationConfig createDecorationConfig(DecorationConfig decorationConfig) {
        this.save(decorationConfig);
        return decorationConfig;
    }

    @Transactional(rollbackFor = Exception.class)
    public DecorationConfig updateDecorationConfig(DecorationConfig decorationConfig) {
        if (ObjectUtil.isNotNull(decorationConfig.getId())) {
            // 删除全部
            this.remove(Wrappers.lambdaQuery(DecorationConfig.class).eq(DecorationConfig::getId, decorationConfig.getId()));
            // 重新添加
            decorationConfig.setId(null);
        }
        return this.createDecorationConfig(decorationConfig);
    }
}
